#
# Makefile template for screen 
#
# See machine dependent config.h for more configuration options.
#

srcdir = @srcdir@
VPATH = @srcdir@

DESTDIR = 

# Where to install screen.

prefix = @prefix@
exec_prefix = @exec_prefix@
datarootdir = @datarootdir@
datadir = @datadir@

# don't forget to change mandir and infodir in doc/Makefile.
bindir  = $(exec_prefix)/bin

VERSION = @PACKAGE_VERSION@
SCREEN = screen-$(VERSION)

DATE_FMT = %Y-%m-%d %H:%m:%S
ifdef SOURCE_DATE_EPOCH
    BUILD_DATE := $(shell date -u -d "@$(SOURCE_DATE_EPOCH)" "+$(DATE_FMT)"  2>/dev/null || date -u -r "$(SOURCE_DATE_EPOCH)" "+$(DATE_FMT)" 2>/dev/null || date -u "+$(DATE_FMT)")
else
    BUILD_DATE := $(shell date "+$(DATE_FMT)")
endif

SCREENENCODINGS = $(datadir)/screen/utf8encodings

CC = @CC@
CFLAGS = @CFLAGS@ -Wall -Wextra -std=c17
CPPFLAGS = @CPPFLAGS@ -iquote. -DSCREENENCODINGS='"$(SCREENENCODINGS)"' -DBUILD_DATE='"$(BUILD_DATE)"'
LDFLAGS = @LDFLAGS@
LIBS = @LIBS@

CPP=@CPP@
CPP_DEPEND=$(CC) -MM

INSTALL = @INSTALL@
INSTALL_PROGRAM = @INSTALL_PROGRAM@
INSTALL_DATA = @INSTALL_DATA@

AWK = @AWK@

SHELL=/bin/sh

CFILES=	screen.c \
	acls.c ansi.c attacher.c backtick.c canvas.c comm.c \
	display.c encoding.c fileio.c help.c input.c kmapdef.c layer.c \
	layout.c list_display.c list_generic.c list_license.o list_window.c logfile.c mark.c \
	misc.c process.c pty.c resize.c sched.c search.c socket.c telnet.c \
	term.c termcap.c tty.c utmp.c viewport.c window.c winmsg.c \
	winmsgbuf.c winmsgcond.c
OFILES=$(CFILES:c=o)

TESTCFILES := $(wildcard tests/test-*.c)
TESTBIN := $(TESTCFILES:.c=)

all:	screen

screen: $(OFILES)
	$(CC) $(LDFLAGS) -o $@ $(OFILES) $(LIBS)

.c.o:
	$(CC) -c $(CPPFLAGS) $(CFLAGS) $< -o $@

check: $(TESTBIN)
	for f in $(TESTBIN); do \
		echo "$$f"; \
		"$$f" || exit $$?; \
	done
tests/test-%: tests/test-%.c %.o tests/mallocmock.o tests/macros.h tests/signature.h
	$(CC) $(CPPFLAGS) $(CFLAGS) $< -o $@ $*.o tests/mallocmock.o

install_bin: screen installdirs
	-if [ -f $(DESTDIR)$(bindir)/$(SCREEN) ] && [ ! -f $(DESTDIR)$(bindir)/$(SCREEN).old ]; \
		then mv $(DESTDIR)$(bindir)/$(SCREEN) $(DESTDIR)$(bindir)/$(SCREEN).old; fi
	$(INSTALL_PROGRAM) screen $(DESTDIR)$(bindir)/$(SCREEN)
	-chown root $(DESTDIR)$(bindir)/$(SCREEN) && chmod 4755 $(DESTDIR)$(bindir)/$(SCREEN)
# This doesn't work if $(bindir)/screen is a symlink
	-if [ -f $(DESTDIR)$(bindir)/screen ] && [ ! -f $(DESTDIR)$(bindir)/screen.old ]; then mv $(DESTDIR)$(bindir)/screen $(DESTDIR)$(bindir)/screen.old; fi
	rm -f $(DESTDIR)$(bindir)/screen
	(cd $(DESTDIR)$(bindir) && ln -f -s $(SCREEN) screen)
	cp $(srcdir)/utf8encodings/?? $(DESTDIR)$(SCREENENCODINGS)

###############################################################################
install: installdirs install_bin
	cd doc ; $(MAKE) install
	-if [ -d $(DESTDIR)/usr/lib/terminfo ]; then \
		PATH="$$PATH:/usr/5bin" tic ${srcdir}/terminfo/screeninfo.src; \
		chmod 644 $(DESTDIR)/usr/lib/terminfo/s/screen*; \
	fi
# Better do this by hand. E.g. under RCS...
#	cat ${srcdir}/terminfo/screencap >> /etc/termcap
	@echo "termcap entry (${srcdir}/terminfo/screencap) should be installed manually."
	@echo "You may also want to install $(srcdir)/etc/etcscreenrc in" $(ETCSCREENRC)

installdirs:
# Path leading to ETCSCREENRC and Socketdirectory not checked.
	$(srcdir)/etc/mkinstalldirs $(DESTDIR)$(bindir) $(DESTDIR)$(SCREENENCODINGS)
	cd doc ; $(MAKE) installdirs

uninstall:
	rm -f $(DESTDIR)$(bindir)/$(SCREEN)
	rm -f $(DESTDIR)$(bindir)/screen
	-mv $(DESTDIR)$(bindir)/screen.old $(DESTDIR)$(bindir)/screen
	rm -f $(DESTDIR)$(ETCSCREENRC)
	cd doc; $(MAKE) uninstall

term.h: term.c term.sh
	AWK=$(AWK) srcdir=$(srcdir) sh $(srcdir)/term.sh

kmapdef.c: term.h

comm.h: comm.c comm.sh config.h term.h
	AWK=$(AWK) CC="$(CC) $(CFLAGS)" srcdir=${srcdir} sh $(srcdir)/comm.sh

docs:
	cd doc; $(MAKE) dvi screen.info

dvi info screen.info:
	-cd doc; $(MAKE) $@

mostlyclean:
	rm -f $(OFILES) screen config.cache

clean: mostlyclean
	rm -f term.h comm.h kmapdef.c core

# Delete everything from the current directory that can be
# reconstructed with this Makefile.
distclean: mostlyclean
	rm -f $(SCREEN).tar $(SCREEN).tar.gz
	rm -f config.status Makefile doc/Makefile
	rm -f term.h comm.h kmapdef.c
	rm -f config.h
	rm -rf autom4te.cache

###############################################################################

mdepend: $(CFILES) term.h
	@rm -f DEPEND ; \
	for i in ${CFILES} ; do \
	  echo "$$i" ; \
	  echo `echo "$$i" | sed -e 's/.c$$/.o/'`": $$i" `\
            cc -E $$i |\
            grep '^# .*"\./.*\.h"' |\
            (sort -t'"' -u -k 2,2 2>/dev/null || sort -t'"' -u +1 -2) |\
            sed -e 's/.*"\.\/\(.*\)".*/\1/'\
          ` >> DEPEND ; \
	done


depend: depend.in
	./config.status || ./configure
	
depend.in: $(CFILES) term.h
	cp Makefile.in Makefile.in~
	sed -e '/\#\#\# Dependencies/q' < Makefile.in > tmp_make
	for i in $(CFILES); do echo $$i; $(CPP_DEPEND) $$i >> tmp_make; done 
	mv tmp_make Makefile.in

###############################################################################

### Dependencies:
screen.o: screen.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h winmsg.h winmsgbuf.h winmsgcond.h backtick.h \
 fileio.h mark.h attacher.h encoding.h help.h misc.h process.h socket.h \
 termcap.h tty.h utmp.h
ansi.o: ansi.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h winmsg.h winmsgbuf.h winmsgcond.h backtick.h encoding.h \
 fileio.h help.h mark.h misc.h process.h resize.h
fileio.o: fileio.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h fileio.h misc.h process.h winmsgbuf.h termcap.h encoding.h
mark.o: mark.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h encoding.h fileio.h mark.h process.h winmsgbuf.h search.h
misc.o: misc.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h
resize.o: resize.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h process.h winmsgbuf.h resize.h telnet.h
socket.o: socket.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h encoding.h fileio.h list_generic.h misc.h process.h \
 winmsgbuf.h resize.h socket.h termcap.h tty.h utmp.h
search.o: search.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h mark.h input.h
tty.o: tty.c config.h screen.h os.h ansi.h sched.h acls.h comm.h layer.h \
 term.h image.h canvas.h display.h layout.h viewport.h window.h logfile.h \
 fileio.h misc.h pty.h telnet.h tty.h
term.o: term.c term.h
window.o: window.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h winmsg.h winmsgbuf.h winmsgcond.h backtick.h fileio.h help.h \
 input.h mark.h misc.h process.h pty.h resize.h telnet.h termcap.h tty.h \
 utmp.h
utmp.o: utmp.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h misc.h tty.h utmp.h
help.o: help.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h misc.h list_generic.h process.h winmsgbuf.h
termcap.o: termcap.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h encoding.h misc.h process.h winmsgbuf.h resize.h termcap.h
input.o: input.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h misc.h
attacher.o: attacher.c config.h screen.h os.h ansi.h sched.h acls.h \
 comm.h layer.h term.h image.h canvas.h display.h layout.h viewport.h \
 window.h logfile.h misc.h socket.h tty.h
pty.o: pty.c config.h screen.h os.h ansi.h sched.h acls.h comm.h layer.h \
 term.h image.h canvas.h display.h layout.h viewport.h window.h logfile.h
process.o: process.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h winmsg.h winmsgbuf.h winmsgcond.h backtick.h encoding.h \
 fileio.h help.h input.h kmapdef.h list_generic.h mark.h misc.h process.h \
 resize.h search.h socket.h telnet.h termcap.h tty.h utmp.h
display.o: display.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h winmsg.h winmsgbuf.h winmsgcond.h backtick.h encoding.h mark.h \
 misc.h process.h pty.h resize.h termcap.h tty.h
comm.o: comm.c config.h os.h screen.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h
kmapdef.o: kmapdef.c config.h
acls.o: acls.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h mark.h misc.h process.h winmsgbuf.h
logfile.o: logfile.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h misc.h
layer.o: layer.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h encoding.h mark.h tty.h
winmsg.o: winmsg.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h winmsg.h winmsgbuf.h winmsgcond.h backtick.h fileio.h \
 process.h mark.h
winmsgbuf.o: winmsgbuf.c winmsgbuf.h screen.h os.h ansi.h sched.h acls.h \
 comm.h layer.h term.h image.h canvas.h display.h layout.h viewport.h \
 window.h logfile.h
winmsgcond.o: winmsgcond.c winmsgcond.h
backtick.o: backtick.c backtick.h screen.h os.h ansi.h sched.h acls.h \
 comm.h layer.h term.h image.h canvas.h display.h layout.h viewport.h \
 window.h logfile.h fileio.h
sched.o: sched.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h
telnet.o: telnet.c config.h comm.h
encoding.o: encoding.c config.h screen.h os.h ansi.h sched.h acls.h \
 comm.h layer.h term.h image.h canvas.h display.h layout.h viewport.h \
 window.h logfile.h encoding.h fileio.h
canvas.o: canvas.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h help.h list_generic.h resize.h
layout.o: layout.c config.h screen.h os.h ansi.h sched.h acls.h comm.h \
 layer.h term.h image.h canvas.h display.h layout.h viewport.h window.h \
 logfile.h fileio.h misc.h process.h winmsgbuf.h resize.h
viewport.o: viewport.c config.h screen.h os.h ansi.h sched.h acls.h \
 comm.h layer.h term.h image.h canvas.h display.h layout.h viewport.h \
 window.h logfile.h
list_display.o: list_display.c config.h screen.h os.h ansi.h sched.h \
 acls.h comm.h layer.h term.h image.h canvas.h display.h layout.h \
 viewport.h window.h logfile.h list_generic.h misc.h
list_generic.o: list_generic.c config.h screen.h os.h ansi.h sched.h \
 acls.h comm.h layer.h term.h image.h canvas.h display.h layout.h \
 viewport.h window.h logfile.h input.h list_generic.h misc.h
list_window.o: list_window.c config.h screen.h os.h ansi.h sched.h acls.h \
 comm.h layer.h term.h image.h canvas.h display.h layout.h viewport.h \
 window.h logfile.h winmsg.h winmsgbuf.h winmsgcond.h backtick.h input.h \
 list_generic.h misc.h process.h
list_license.o: list_license.c list_generic.h misc.h comm.h
